#python -u "d:\nyahentai\DoujinshiInfotoMarkdownYGO2.py"
import os
import sys

def getCheckBracket(info):
    if "[" not in info:
        return [False,False]
    if info.count("[") == 1:
        if info[len(info)-1] == "]":
            return [False,True]
        else:
            return [True,False]
    else:
        return [True,True]

def getType(bracket0,bracket1,info):
    if bracket0 == bracket1:
        if bracket0 == True:#tt
            if "(" in info[info.find("["):info.find("]")]:
                info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
            if "(" not in info:
                return 232
            elif info.count("(") == 1:
                if info[0] == "(":
                    return 1232
                else:
                    return 2312
            else:
                return 12312
        else:#ff
            if "(" not in info:
                return 3
            elif info.count("(") == 1:
                if info[0] == "(":
                    return 13
                else:
                    return 31
            else:
                return 131
 
    if bracket0 != bracket1:
        if bracket0 == True:#tf
            if "(" in info[info.find("["):info.find("]")]:
                info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
            if "(" not in info:
                return 23
            elif info.count("(") == 1:
                if info[0] == "(":
                    return 123
                else:
                    return 231
            else:
                return 1231
        else:#ft
            if "(" not in info:
                return 32
            elif info.count("(") == 1:
                if info[0] == "(":
                    return 132
                else:
                    return 312
            else:
                return 1312

    '''
    tt
    info "()[]doujinshi()[]"12doujinshi12
    info = "()[]doujinshi[]"12doujinshi2
    info = "[]doujinshi()[]"2doujinshi12
    info = "[]doujinshi[]"2doujinshi2
    ff
    info = "()doujinshi()"11
    info = "()doujinshi"1doujinshi0
    info = "doujinshi()"0doujinshi1
    info = "doujinshi"0
    '''
    '''
    tf
    info = "()[]doujinshi()"12doujinshi1
    info = "()[]doujinshi"12doujinshi0
    info = "[]doujinshi()"2doujinshi1
    info = "[]doujinshi"2doujinshi0
    ft
    info = "()doujinshi()[]"1doujinshi12
    info = "()doujinshi[]"1doujinshi2
    info = "doujinshi()[]"0doujinshi12
    info = "doujinshi[]"0doujinshi2
    '''

def getInfo(type,info):
    if type ==3:
        return [info,"",""]
    elif type ==32:
        return [info[:info.rfind("[")],"",""]
    elif type ==31:
        return [info[:info.rfind("(")],"",info[info.rfind("(")+1:info.rfind(")")]]
    elif type ==312:
        return [info[:info.rfind("(")],"",info[info.rfind("(")+1:info.rfind(")")]]
    elif type ==23:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:],info[info.find("[")+1:info.find("]")],""]
    elif type ==232:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:info.rfind("[")],info[info.find("[")+1:info.find("]")],""]
    elif type ==231:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:info.rfind("(")],info[info.find("[")+1:info.find("]")],info[info.rfind("(")+1:info.rfind(")")]]
    elif type ==2312:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:info.rfind("(")],info[info.find("[")+1:info.find("]")],info[info.rfind("(")+1:info.rfind(")")]]
    elif type ==13:
        return [info[info.find(")")+1:],"",""]
    elif type ==132:
        return [info[info.find(")")+1:info.rfind("[")],"",""]
    elif type ==131:
        return [info[info.find(")")+1:info.rfind("(")],"",info[info.rfind("(")+1:info.rfind(")")]]
    elif type ==1312:
        return [info[info.find(")")+1:info.rfind("(")],"",info[info.rfind("(")+1:info.rfind(")")]]
    elif type ==123:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find(")")+1:],info[info.find("[")+1:info.find("]")],""]
    elif type ==1232:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:info.rfind("[")],info[info.find("[")+1:info.find("]")],""]
    elif type ==1231:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:info.rfind("(")],info[info.find("[")+1:info.find("]")],info[info.rfind("(")+1:info.rfind(")")]]
    else:
        if "(" in info[info.find("["):info.find("]")]:
            info = info[:info.find("(")]+"（"+info[info.find("(")+1:info.find(")")]+"）"+info[info.find(")")+1:]
        return [info[info.find("]")+1:info.rfind("(")],info[info.find("[")+1:info.find("]")],info[info.rfind("(")+1:info.rfind(")")]]
path = "D:\\nyahentai\\"+sys.argv[1]
print(path+".md")
urls = []
doujinshis = []
ThumbUrls = []
authors = []
parodies = []
FileList = os.listdir(path)
for file in FileList:
    f2 = open(path+"\\"+file,"r",encoding="utf-8")
    LineList = f2.readlines()
    f2.close()
    for line in LineList:
        if "/g/" in line:
            str1 = line[line.find("/"):line.find("\" class")]
            urls.append("https://zhb.doghentai.com"+str1)
            continue
        elif "data-src" in line:
            info = line[line.find("alt=\"")+5:line.rfind("\"")]
            if info == "":
                doujinshis.append(" ")
            else:
                Bracket = getCheckBracket(info)
                Type = getType(Bracket[0],Bracket[1],info)
                doujinshis.append(getInfo(Type,info)[0])
            ThumbUrl = line[line.find("data-src=\"")+10:line.find(".jpg")+4]
            ThumbUrls.append("![thumb.jpg]("+ThumbUrl+")|")
            continue
#f1 = open(path+".md","w",encoding="utf-8")
#f1.close()
count = len(urls)//4
for i in range(len(urls)-count*4):
    urls.append(" ")
    doujinshis.append(" ")
    ThumbUrls.append(" ")
f1 = open(path+".md","w",encoding="utf-8")
f1.write("| 1 | 2 | 3 | 4 |\n| ------ | ------ | ------ | ------ |\n")
for i in range(count):
    f1.write("|"+ThumbUrls[i*4+0]+ThumbUrls[i*4+1]+ThumbUrls[i*4+2]+ThumbUrls[i*4+3]+"\n")
    f1.write("|["+doujinshis[i*4+0]+"]("+urls[i*4+0]+")|["+doujinshis[i*4+1]+"]("+urls[i*4+1]+")|["+doujinshis[i*4+2]+"]("+urls[i*4+2]+")|["+doujinshis[i*4+3]+"]("+urls[i*4+3]+")|\n")
f1.close()